//
//  MusicTimeTableViewController.m
//  product_B
//
//  Created by zyc on 16/5/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MusicTimeTableViewController.h"
#import "MusicModel.h"
#import "MusicAuthorModel.h"
#import "MusicTimeTableViewCell.h"
#import "MusicDetailWebViewController.h"
#import "MusicListViewController.h"
@interface MusicTimeTableViewController ()
@property (nonatomic, strong)NSMutableArray *musicArray;
@property (nonatomic, strong)NSString *urlString;
@end

@implementation MusicTimeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.rowHeight = 60;
   
    self.navigationController.navigationBar.translucent = NO;
    _musicArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"MusicTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self request];
}

- (void)request{
    if (_time != nil && _musicID == nil && _authorID == nil) {
        self.navigationItem.title = _time;
        _urlString = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/music/bymonth/%@",_time];
    }
    if (_time == nil && _musicID != nil && _authorID == nil){
        self.navigationItem.title = @"歌单";
        _urlString = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/othercollection/%@/more/4/0",_musicID];
    }
    if (_time == nil && _musicID == nil && _authorID != nil) {
        self.navigationItem.title = @"ta的歌单";
        _urlString = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/works/music/%@",_authorID];
    }
    [RequestManager initWithUrlString:_urlString requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = jsonDic[@"data"];
        if (array.count == 0) {
            NSLog(@"没有参数");
            return ;
        }
        
        for (NSDictionary *dic in array) {
            MusicModel *model = [[MusicModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [_musicArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } errorBlock:^(NSError *error) {
        
    }];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _musicArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MusicTimeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    MusicModel *model = _musicArray[indexPath.row];
    MusicAuthorModel *author = model.author;
    
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:author.web_url]];
    cell.title.text = model.title;
    cell.author.text = author.user_name;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    MusicDetailWebViewController *webView = [[MusicDetailWebViewController alloc]init];
    MusicModel *model = _musicArray[indexPath.row];
    //    webView.name = model.title;
    //    webView.myID = model.myID;
    //    [self.navigationController pushViewController:webView animated:YES];
    MusicListViewController *listView = [[MusicListViewController alloc]init];
    listView.isChild = YES;
    listView.musicID = model.myID;
    [self.navigationController pushViewController:listView animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}



/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
